﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace RaisingStudio.Data.Common
{
    public delegate void LogWrapperWrite(object value);

    public class LogMethodWrapper : TextWriter
    {
        private LogWrapperWrite _writeDelegate;
        public LogWrapperWrite WriteDelegate
        {
            get { return _writeDelegate; }
            set { _writeDelegate = value; }
        }

        public LogMethodWrapper()
        {
        }

        public LogMethodWrapper(LogWrapperWrite logWrapperWrite)
        {
            this._writeDelegate = logWrapperWrite;
        }


        protected override void Dispose(bool disposing)
        {
            this._writeDelegate = null;
            base.Dispose(disposing);
        }


        private static UnicodeEncoding m_encoding;
        public override Encoding Encoding
        {
            get
            {
                if (m_encoding == null)
                {
                    m_encoding = new UnicodeEncoding(false, false);
                }
                return m_encoding;
            }
        }

        public override void Write(char value)
        {
            if (this._writeDelegate != null)
            {
                this._writeDelegate(value);
            }
        }

        public override void Write(string value)
        {
            if (this._writeDelegate != null)
            {
                this._writeDelegate(value);
            }
        }

        public override void Write(char[] buffer, int index, int count)
        {
            Write(new string(buffer, index, count));
        }
    }
}
